#include "RunState.h"
#include "utils\Timer.h"
#include "utils\Log.h"
#include "base\Engine.h"
#include "Constants.h"

RunState::RunState(std::string name,GameCore* core) : GameState(name) , m_Core(core) {
	gEngine->getEventManager().registerEventHandler(this);
	gEngine->addController(this);	
}

void RunState::switchState(GameMode state) {
	m_State = state;
	if ( state == INTRO ) {
		gEngine->getWorld().activateLayerByName("StartMenu");
		gEngine->getActionManager().startAction("IntroMenu");	
	}
	else if ( state == MENU ) {
		gEngine->getAudioManager().play("Opium",100,true);
		m_Core->grid().clear();
		gEngine->getWorld().activateLayerByName("StartMenu");
		gEngine->getActionManager().startAction("MainMenu");	
	}	
	else if ( state == CREDITS ) {
		gEngine->getWorld().activateLayerByName("StartMenu");
		gEngine->getActionManager().startAction("CreditsMenu");		
	}	
	else if ( state == PLAYING ) {
		gEngine->getAudioManager().stop("Opium");
		m_Core->grid().create(m_Core->getLevel());
		m_Core->hud().reset();
		m_Core->resetScore();
		Highscore hs;
		if ( m_Core->highscores().get(m_Core->getLevel(),hs) ) {
			LOG(logINFO) << "found highscore " << hs.points;
			m_Core->score().highscore = hs.points;
		}
		gEngine->getAudioManager().stop("Opium");
	}
	else if ( state == GAME_OVER ) {
		//gEngine->getActionManager().startAction("Fade");
		m_Core->grid().setActive(false);		
		m_Core->grid().clear();		
		m_Core->hud().deactivate();		
		m_Core->highscores().setScore(m_Core->getLevel(),m_Core->score().points,m_Core->hud().getSeconds(),m_Core->hud().getMinutes());
		gEngine->getWorld().activateLayerByName("EndMenu");
		gEngine->getActionManager().startAction("GameOverMenu");		
		gEngine->getAudioManager().play("Opium",100,true);					
	}
}

bool RunState::update(float elapsedTime) {	
	m_Core->grid().update(elapsedTime);			
	return true;
}

bool RunState::onActivation() {
	LOG(logINFO) << "Activating Run state";
	running = true;
	ShowCursor(false);
	switchState(MENU);
	m_Core->hud().deactivate();
	return true;
}
bool RunState::onDeactivation() {
	return true;
}

void RunState::process(ds::EventBuffer& buffer ) {	
	if ( buffer.containsID(10) ) {
		gEngine->getActionManager().stopAction("MainMenu");
		switchState(PLAYING);
	}	

	if ( buffer.containsID(20) ) {
		m_Core->setLevel(4);
		gEngine->getActionManager().stopAction("LevelSelectionMenu");
		switchState(PLAYING);
	}	
	if ( buffer.containsID(21) ) {
		m_Core->setLevel(5);
		gEngine->getActionManager().stopAction("LevelSelectionMenu");
		switchState(PLAYING);
	}	
	if ( buffer.containsID(22) ) {
		m_Core->setLevel(6);
		gEngine->getActionManager().stopAction("LevelSelectionMenu");
		switchState(PLAYING);
	}	

	if ( buffer.containsID(EVN_GAME_OVER) ) {
		switchState(GAME_OVER);
	}	
	if ( buffer.containsID(66) ) {
		gEngine->shutdown();
	}	
	if ( buffer.containsID(EVN_QUIT_GAME)) {
		switchState(MENU);		
	}
	if ( buffer.containsID(EVN_LEVEL_READY) ) {
		m_Core->hud().activate(false);
	}	
	if ( buffer.containsID(EVN_ADD_SCORE)) {
		int points;
		buffer.getByID(&points,sizeof(int),EVN_ADD_SCORE);
		m_Core->score().points += points * points * 10;
		if ( points > m_Core->score().highestCombo ) {
			m_Core->score().highestCombo = points;
		}
		m_Core->hud().setScore(m_Core->score().points);
		m_Core->score().piecesLeft -= points;
		m_Core->score().totalPieces += points;
		if ( m_Core->score().points > m_Core->score().highscore ) {
			m_Core->score().highscore = m_Core->score().points;
		}
	}
}



